
package ac.uk.qmul.gemom.scenario;
import java.io.*;
import java.util.*;

/* 
 * COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
 * @version 1.0 
 */

public class TextFile extends ArrayList {
  // Tools to read and write files as single strings:
  public static String
  read(String fileName) throws IOException {
    StringBuffer sb = new StringBuffer();
    BufferedReader in =
      new BufferedReader(new FileReader(fileName));
    String s;
    while((s = in.readLine()) != null) {
      sb.append(s);
      sb.append("\n");
    }
    in.close();
    return sb.toString();
  }
  
  public static void
  write(String fileName, String text) throws IOException {
    PrintWriter out = new PrintWriter(
      new BufferedWriter(new FileWriter(fileName)));
    out.print(text);
    out.close();
  }
  
  public TextFile(String fileName) throws IOException {
    super(Arrays.asList(read(fileName).split("\n")));
  }
  
  public TextFile(ArrayList<String> a){
	  super(a);
  }
  public void write(String fileName) throws IOException {
    PrintWriter out = new PrintWriter(
      new BufferedWriter(new FileWriter(fileName)));
    for(int i = 0; i < size(); i++)
      out.println(get(i));
    out.close();
  }
  
  // Simple test:
  public static void main(String[] args) throws Exception {
/*    String file = read("C:\\w\\NaiveBayesClassifier_bob\\ScenaroDemo\\src\\ac\\uk\\qmul\\gemom\\scenario\\TextFile.java");
    write("C:\\w\\NaiveBayesClassifier_bob\\ScenaroDemo\\src\\ac\\uk\\qmul\\gemom\\scenario\\test.txt", file);
    TextFile text = new TextFile("C:\\w\\NaiveBayesClassifier_bob\\ScenaroDemo\\src\\ac\\uk\\qmul\\gemom\\scenario\\test.txt");
    text.write("C:\\w\\NaiveBayesClassifier_bob\\ScenaroDemo\\src\\ac\\uk\\qmul\\gemom\\scenario\\test.txt");
 */ 
	write("C:\\Program Files (x86)\\Sulley Fuzzing Framework\\test.lrn", "% 9\t1\t1\t1");
	System.out.println();
  }
} ///:~